package com.obtk.property.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.obtk.property.dao.ComplaintMapper;
import com.obtk.property.pojo.Complaint;
import com.obtk.property.service.ComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class ComplaintServiceImpl implements ComplaintService {
    @Autowired
    private ComplaintMapper complaintMapper;
    @Override
    public List<Complaint> findAll() {
        return complaintMapper.selectAll();
    }
    @Override
    public List<Complaint> selB() {
        return complaintMapper.selB();
    }
    @Override
    public List<Complaint> selC(String name) {
        return complaintMapper.selC(name);
    }
    @Override
    public Page<Complaint> search(Map searchMap) {
        Example example = new Example(Complaint.class);
        int pageNum = 1;
        int pageSize = 2;
        if(searchMap != null){
            Example.Criteria criteria = example.createCriteria();//创建查询条件
            //时间区间
            if(StringUtil.isNotEmpty((String) searchMap.get("startTime"))){
                criteria.andGreaterThanOrEqualTo("createTime",searchMap.get("startTime"));
            }
            if(StringUtil.isNotEmpty((String) searchMap.get("endTime"))){
                criteria.andLessThanOrEqualTo("createTime",searchMap.get("endTime"));
            }
            //名称模糊搜索
            if(StringUtil.isNotEmpty((String) searchMap.get("ownerName"))){
                criteria.andLike("ownerName", "%"+(String) searchMap.get("ownerName")+"%");
            }
            //分页

            if((Integer) searchMap.get("pageNum") !=null){
                pageNum = (Integer) searchMap.get("pageNum");
            }
            if((Integer) searchMap.get("pageSize") !=null){
                pageSize = (Integer) searchMap.get("pageSize");
            }
        }
        PageHelper.startPage(pageNum,pageSize);//使用PageHelper插件完成分页
        Page<Complaint> Complaint = (Page<Complaint>) complaintMapper.selectByExample(example);
        return Complaint;
    }

    @Override
    public Boolean add(Complaint complaint) {
        int row=complaintMapper.insert(complaint);
        if(row>0){
            return true;
        }
        return false;
    }

    @Override
    public Complaint findById(Integer complaintId) {
        return complaintMapper.selectByPrimaryKey(complaintId);
    }

    @Override
    public Boolean update(Complaint complaint) {
        int row=complaintMapper.updateByPrimaryKeySelective(complaint);
        if(row>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean del(List<Integer> ids) {
        for(Integer id:ids){
            complaintMapper.deleteByPrimaryKey(id);
        }
        return true;
    }

    @Override
    public Boolean updateStatus(String status, Integer complaintId) {
        Complaint complaint = new Complaint();
        complaint.setComplaintId(complaintId);
        complaint.setStatus(status);
        int row = complaintMapper.updateByPrimaryKeySelective(complaint);
        if(row>0){
            return true;
        }else{
            return false;
        }

    }

    @Override
    public Integer selectCount(Complaint complaint) {
        return complaintMapper.selectCount(complaint);
    }
}
